<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\facade\Cache;
use app\common\validate\MouldsFieldValidate;
use app\admin\model\ModelsField as fieldModel;
class MouldsField extends Common {
	
	protected function initialize()
	{
	    parent::initialize();
	    $this->fieldModel = new fieldModel;
	}
	
	//模型字段列表
	public function index(){
		$id = request()->param('id');
		$limit = request()->param('limit');
		$page = request()->param('page');
		$start = ($page-1)*$limit;
		$count = db('models_field')
				->alias('a')
				->join('field_type b','a.type=b.id')
				->where('model_id',$id)
				->where('show',1)
				->count();
		$result =db('models_field')
				->alias('a')
				->join('field_type b','a.type=b.id')
				->where('model_id',$id)
				->where('show',1)
				->limit($start,$limit)
				->order('a.sort asc,a.id desc')
				->field('a.*,b.name,b.title')
				->cache('models_field'.$id.$limit.$page,3600,'models_field')
				->select();
		
		return showTable(0, 'OK', $count ,$result, 200);
	}
	//字段添加
	public function fieldadd(){
		if(request()->isPost()){
			$data = request()->param();
			$validate = new MouldsFieldValidate;
			if (!$validate->scene('add')->check($data)) {
				return show(0, $validate->getError(), 0 , 200);
			}else{
				if($this->fieldModel->addModel($data)){
					Cache::clear('models');
					Cache::clear('models_field');
					return show(1, lang('add success'),"", 200);
				}
				
				return show(0, $this->fieldModel->getError(), $this->fieldModel->getError(), 200);
			}
		}
		$type = db('field_type')->select();
		$this->assign('type',$type);
		return $this->fetch();
	}
	//获取字段选项
	public function getField(){
		$data = request()->param();
		$tpl = get_feild($data['field_type']);
		return $tpl;
	}
	//删除字段
	public function del(){
		if(request()->isPost()){
			$data = request()->param();
			if ($this->fieldModel->deleteField($data)) {
				Cache::clear('models');
				Cache::clear('models_field');
			    return show(config('code.success'), 'OK', "{}", 200);
			} else {
				Cache::clear('models');
				Cache::clear('models_field');
				return show(config('code.error'), 'error', $this->fieldModel->getError(), 200);
			}
		}
		
	}
	public function edit(){
		if(request()->isPost()){
			$data = request()->param();
			if ($this->fieldModel->editField($data)) {
				Cache::clear('models');
				Cache::clear('models_field');
			    return show(config('code.success'), '修改成功', "{}", 200);
			} else {
				Cache::clear('models');
				Cache::clear('models_field');
				return show(config('code.error'), 'error', $this->fieldModel->getError(), 200);
			}
		}
		$data = request()->param();
		$rest = db('models_field')->where(['id'=>$data['id']])->find();
		$type = db('field_type')->select();
		$this->assign('type',$type);
		$this->assign('rest',$rest);
		return $this->fetch();
	}
	// 字段排序
	public function sort(){
		if(request()->isPost()){
			$data = request()->param();
			$validate = new MouldsFieldValidate;
			
			if (!$validate->scene('sort')->check($data)) {
				return show(config('code.error'), 'error', $validate->getError(), 200);
			}else{
				Cache::clear('models');
				Cache::clear('models_field');
				$rest = (new fieldModel())->save(['sort'=>$data['sort']],['id'=>$data['id']]);
				return show(config('code.success'), 'OK', $rest, 200);
			}
		}
	}
	
}
